package com.lizhi.yun.webservice.impl;

import com.lizhi.utils.StringUtils;
import com.lizhi.utils.json.SamJsonUtil;
import com.lizhi.utils.tools.BeanCopyUtil;
import com.lizhi.yun.db.sfrobot.entity.*;
import com.lizhi.yun.db.sfrobot.repository.*;
import com.lizhi.yun.webservice.dto.DoctorMessageResult;
import com.lizhi.yun.webservice.dto.PatientMessageResult;
import com.lizhi.yun.webservice.WebService;
import com.lizhi.yun.webservice.dto.doctor.*;
import com.lizhi.yun.webservice.dto.paient.*;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: lizhi
 * @Description:
 * @Date: create in 2020/12/15 13:20
 */
@javax.jws.WebService(serviceName = "WebService", //与接口中指定的name一直
        targetNamespace = "http://webservice.yun.lizhi.com/", // 与接口中的命名空间一致,一般是接口的包名倒
        endpointInterface = "com.lizhi.yun.webservice.WebService"//接口地址
)
@Component
public class WebServiceImpl implements WebService {

    @Resource
    private DoctorRepo doctorRepo;

    @Resource
    private PatientRepo patientRepo;

    @Resource
    private AssessRecordRepo assessRecordRepo;

    @Resource
    private AssessTableRepo assessTableRepo;

    @Resource
    private TrainingRecordRepo trainingRecordRepo;

    @Resource
    private PrescriptionActionRepo prescriptionActionRepo;

    @Resource
    private GameRecordRepo gameRecordRepo;

    /**
     * 查询患者信息
     * @param patientName 患者姓名
     * @param certificateNo 患者证件号
     * @return
     */
    @Override
    public String queryPatientMessage(String patientName, String certificateNo) {
        PatientMessageResult messageResult = new PatientMessageResult();

        /*获取到患者信息*/
        List<Patient> patients = patientRepo.findAllByNameAndCertificateNoOrderByCreatedTimeDesc(patientName, certificateNo);
        List<PatientDto> patientDtos = new ArrayList<>();
        for (Patient patient : patients){
            PatientDto patientDto = new PatientDto();

            /*患者信息*/
            Doctor doctor = doctorRepo.findByIdAndHospitalIdAndHospitalSystemId(Long.parseLong(patient.doctorId), patient.getHospitalId(), patient.getHospitalSystemId());
            BeanCopyUtil.copy(patient, patientDto);
            if (!StringUtils.isEmpty(doctor)){
                patientDto.setDoctorAccount(doctor.account);
                patientDto.setDoctorName(doctor.name);
            }

            /*获取评估信息*/
            List<AssessRecord> assessRecords = assessRecordRepo.queryAllByPatientIdAndHospitalIdAndHospitalSystemIdOrderByCreatedTimeDesc(patient.id, patient.getHospitalId(), patient.getHospitalSystemId());
            List<PatientAssessDto> patientAssessDtos = new ArrayList<>();
            for (AssessRecord assessRecord : assessRecords){
                PatientAssessDto patientAssessDto = new PatientAssessDto();
                BeanCopyUtil.copy(assessRecord, patientAssessDto);
                List<AssessTable> assessTables = assessTableRepo.queryAllByRecordIdAndHospitalIdAndHospitalSystemIdOrderByIllnessSideAsc(Long.parseLong(assessRecord.getId()), assessRecord.getHospitalId(), assessRecord.getHospitalSystemId());
                List<PatientAssessTableDto> patientAssessTableDtos = new ArrayList<>();
                for (AssessTable assessTable : assessTables){
                    PatientAssessTableDto patientAssessTableDto = new PatientAssessTableDto();
                    BeanCopyUtil.copy(assessTable, patientAssessTableDto);
                    patientAssessTableDtos.add(patientAssessTableDto);
                }
                patientAssessDto.setPatientAssessTableDtos(patientAssessTableDtos);
                patientAssessDtos.add(patientAssessDto);
            }
            patientDto.setPatientAssessDtos(patientAssessDtos);

            /*处方信息*/
            List<TrainingRecord> trainingRecords = trainingRecordRepo.queryAllByPatientIdAndHospitalIdAndHospitalSystemIdOrderByCreatedTimeDesc(patient.id, patient.getHospitalId(), patient.getHospitalSystemId());
            List<PatientPrescriptionDto> patientPrescriptionDtos = new ArrayList<>();
            for (TrainingRecord trainingRecord : trainingRecords){
                PatientPrescriptionDto patientPrescriptionDto = new PatientPrescriptionDto();
                BeanCopyUtil.copy(trainingRecord, patientPrescriptionDto);
                List<PrescriptionAction> prescriptionActions = prescriptionActionRepo.findAllByPrescriptionIdAndHospitalIdAndHospitalSystemIdOrderBySortIndexAsc(Long.parseLong(trainingRecord.getPrescriptionId()), trainingRecord.getHospitalId(), trainingRecord.getHospitalSystemId());
                List<PatientPrescriptionActionDto> patientPrescriptionActionDtos = new ArrayList<>();
                for (PrescriptionAction prescriptionAction : prescriptionActions){
                    PatientPrescriptionActionDto patientPrescriptionActionDto = new PatientPrescriptionActionDto();
                    BeanCopyUtil.copy(prescriptionAction, patientPrescriptionActionDto);
                    patientPrescriptionActionDtos.add(patientPrescriptionActionDto);
                }
                patientPrescriptionDto.setPatientPrescriptionActionDtos(patientPrescriptionActionDtos);
                patientPrescriptionDtos.add(patientPrescriptionDto);
            }
            patientDto.setPatientPrescriptionDtos(patientPrescriptionDtos);

            /*游戏信息*/
            List<GameRecord> gameRecords = gameRecordRepo.queryAllByPatientIdAndHospitalIdAndHospitalSystemIdOrderByCreatedTimeDesc(patient.id, patient.getHospitalId(), patient.getHospitalSystemId());
            List<PatientGameDto> patientGameDtos = new ArrayList<>();
            for (GameRecord gameRecord : gameRecords){
                PatientGameDto patientGameDto = new PatientGameDto();
                BeanCopyUtil.copy(gameRecord, patientGameDto);
                patientGameDtos.add(patientGameDto);
            }
            patientDto.setPatientGameDtos(patientGameDtos);
            /*将患者信息放入集合中*/
            patientDtos.add(patientDto);
        }

        messageResult.setData(patientDtos);
        messageResult.setCode("000000");
        messageResult.setMesage("成功");
        return SamJsonUtil.toJson(messageResult);
    }

    /**
     * 查询医师信息
     * @param doctorAccount 医师账号
     * @param hospitalId 医院id
     * @return
     */
    @Override
    public String queryDoctorMessage(String doctorAccount, String hospitalId) {
        DoctorMessageResult messageResult = new DoctorMessageResult();
        /*查询该医师的信息*/
        List<Doctor> doctors = doctorRepo.findAllByAccountAndHospitalId(doctorAccount, hospitalId);
        if (doctors.size() < 1){
            messageResult.setCode("999999");
            messageResult.setMesage("医师账号不存在");
            return SamJsonUtil.toJson(messageResult);
        }
//        if (doctors.size() > 1){
//            messageResult.setCode("999998");
//            messageResult.setMesage("医师账号存在多个");
//            return SamJsonUtil.toJson(messageResult);
//        }
        List<Patient> listPatients = new ArrayList<>();
        for (Doctor doctor : doctors){
            /*查询该医师治疗的所有患者*/
            List<Patient> patients = patientRepo.findAllByDoctorIdAndHospitalIdOrderByCreatedTimeDesc(doctor.getId().toString(), hospitalId);
            listPatients.addAll(patients);
        }
        List<DoctorDto> doctorDtos = new ArrayList<>();
        for (Patient patient : listPatients){
            DoctorDto doctorDto = new DoctorDto();
            /*患者信息*/
            DoctorPatientDto doctorPatientDto = new DoctorPatientDto();
            BeanCopyUtil.copy(patient, doctorPatientDto);
            doctorDto.setDoctorPatientDto(doctorPatientDto);
            /*评估信息*/
            List<DoctorAssessDto> doctorAssessDtos = new ArrayList<>();
            List<AssessRecord> assessRecords = assessRecordRepo.queryAllByPatientIdAndHospitalIdAndHospitalSystemIdOrderByCreatedTimeDesc(patient.getId(), patient.getHospitalId(), patient.getHospitalSystemId());
            for (AssessRecord assessRecord : assessRecords){
                DoctorAssessDto doctorAssessDto = new DoctorAssessDto();
                doctorAssessDto.setCreatedTime(assessRecord.createdTime);
                List<AssessTable> assessTables = assessTableRepo.queryAllByRecordIdAndHospitalIdAndHospitalSystemIdOrderByIllnessSideAsc(Long.parseLong(assessRecord.getId()), assessRecord.getHospitalId(), assessRecord.getHospitalSystemId());
                List<DoctorAssessTableDto> doctorAssessTableDtos = new ArrayList<>();
                for (AssessTable assessTable : assessTables){
                    DoctorAssessTableDto doctorAssessTableDto = new DoctorAssessTableDto();
                    BeanCopyUtil.copy(assessTable, doctorAssessTableDto);
                    doctorAssessTableDtos.add(doctorAssessTableDto);
                }
                doctorAssessDto.setDoctorAssessTableDtos(doctorAssessTableDtos);
                doctorAssessDtos.add(doctorAssessDto);
            }
            doctorDto.setDoctorAssessDtos(doctorAssessDtos);
            /*处方信息*/
            List<DoctorPrescriptionDto> doctorPrescriptionDtos = new ArrayList<>();
            List<TrainingRecord> trainingRecords = trainingRecordRepo.queryAllByPatientIdAndHospitalIdAndHospitalSystemIdOrderByCreatedTimeDesc(patient.getId(), patient.getHospitalId(), patient.getHospitalSystemId());
            for (TrainingRecord trainingRecord : trainingRecords){
                DoctorPrescriptionDto doctorPrescriptionDto = new DoctorPrescriptionDto();
                BeanCopyUtil.copy(trainingRecord, doctorPrescriptionDto);
                List<PrescriptionAction> prescriptionActions = prescriptionActionRepo.findAllByPrescriptionIdAndHospitalIdAndHospitalSystemIdOrderBySortIndexAsc(Long.parseLong(trainingRecord.getPrescriptionId()), trainingRecord.getHospitalId(), trainingRecord.getHospitalSystemId());
                List<DoctorPrescriptionActionDto> doctorPrescriptionActionDtos = new ArrayList<>();
                for (PrescriptionAction prescriptionAction : prescriptionActions){
                    DoctorPrescriptionActionDto doctorPrescriptionActionDto = new DoctorPrescriptionActionDto();
                    BeanCopyUtil.copy(prescriptionAction, doctorPrescriptionActionDto);
                    doctorPrescriptionActionDtos.add(doctorPrescriptionActionDto);
                }
                doctorPrescriptionDto.setDoctorPrescriptionActionDtos(doctorPrescriptionActionDtos);
                doctorPrescriptionDtos.add(doctorPrescriptionDto);
            }
            doctorDto.setDoctorPrescriptionDtos(doctorPrescriptionDtos);

            /*游戏记录*/
            List<GameRecord> gameRecords = gameRecordRepo.queryAllByPatientIdAndHospitalIdAndHospitalSystemIdOrderByCreatedTimeDesc(patient.getId(), patient.getHospitalId(), patient.getHospitalSystemId());
            List<DoctorGameDto> doctorGameDtos = new ArrayList<>();
            for (GameRecord gameRecord : gameRecords){
                DoctorGameDto doctorGameDto = new DoctorGameDto();
                BeanCopyUtil.copy(gameRecord, doctorGameDto);
                doctorGameDtos.add(doctorGameDto);
            }
            doctorDto.setDoctorGameDtos(doctorGameDtos);
            /*全部放入到集合中*/
            doctorDtos.add(doctorDto);
        }
        messageResult.setData(doctorDtos);
        messageResult.setCode("000000");
        messageResult.setMesage("成功");
        return SamJsonUtil.toJson(messageResult);
    }
}
